package com.siemens.industrial.web.mvc.service;

import com.siemens.industrial.model.admin.SystemConfig;
import com.siemens.industrial.utils.JsonToOtherFormat;
import com.siemens.industrial.utils.SiemensFileWriter;
import com.siemens.industrial.web.mvc.service.admin.SystemConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by qupen on 2016/12/26.
 */
@Service
public class WriteLogs {
    @Resource
    SystemConfigService systemConfigService;

    public void writeLog(String type,String content,String database){
        SystemConfig systemConfig = systemConfigService.getOneByConfigKey(SystemConfig.LOGS_CONFIG_KEY);
        if (systemConfig != null) {
            Map<String, Object> logs = JsonToOtherFormat.buildJsonToMap(systemConfig.getSystemValue());
            if (logs != null) {
                String path = String.valueOf(logs.get(type));//type
                if (SiemensFileWriter.checkPath(path)) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    SiemensFileWriter.writeToFile(path,sdf.format(new Date())+" -"+database,content, true);
                }
            }
        }
    }
}
